﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Business;
using Business.DbConnect;
using PagedList;

namespace ServerMotorManagement.Controllers
{
    [Authorize]
    public class TicketController : Controller
    {
        //
        // GET: /Ticket/

        public ActionResult Index(int? page)
        {
            int pageNumber = (page ?? 1);
            var ticket = TicketBusiness.Search(String.Empty);
            return View(ticket.ToPagedList(pageNumber, 3));
        }

        public ActionResult ListTicket(int? page)
        {
            var ticket = TicketBusiness.Search(String.Empty);
            int pageNumber = (page ?? 1);
            return PartialView("ListTicket", ticket.ToPagedList(pageNumber, 3));
        }

        public ActionResult Create()
        {
            IEnumerable<SelectListItem> sList = new SelectList(TicketTypeBusiness.ListTicketTypes(), "Id", "Description");
            ViewBag.TicketType = sList;
            return PartialView("Create");
        }

        /// <summary>
        /// Search by Content
        /// </summary>
        /// <param name="sContent"></param>
        /// <returns></returns>

        [HttpPost]
        public ActionResult Search(String sContent, string currentFilter, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                sContent = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = sContent;
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            var ticket = TicketBusiness.Search(sContent);
            return PartialView("ListTicket", ticket.ToPagedList(pageNumber, pageSize));
        }

        public JsonResult AutoCompleteSuggestionCustomer(string term)
        {
            return Json(CustomerBusiness.Suggestion(term), JsonRequestBehavior.AllowGet);
        }

        public JsonResult AutoCompleteSuggestionMotorbike(string term)
        {
            return Json(MotorBusiness.Suggestion(term), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Create(Ticket tk, FormCollection collection)
        {
            tk.CustomerId = CustomerBusiness.SelectIdbyName(collection["SearchCustomer"]);
            IFormatProvider iFP = new System.Globalization.CultureInfo("vi-VN", true);

            tk.StartTime = DateTime.Parse(collection["StartTime"], iFP);
            tk.EndTime = DateTime.Parse(collection["EndTime"], iFP);
            tk.Price = 0;
            tk.MotorbikePlate = collection["SearchPlate"];
            tk.TypeId = Int64.Parse(collection["ticketType"]);
            TicketBusiness.Insert(tk);
            IEnumerable<SelectListItem> sList = new SelectList(TicketTypeBusiness.ListTicketTypes(), "Id", "Description");
            ViewBag.TicketType = sList;
            return PartialView("Create");
        }

        public ActionResult Edit(int id)
        {
            Ticket tk = TicketBusiness.Select(id);
            ViewBag.ticketType = new SelectList(TicketTypeBusiness.ListTicketTypes(), "Id", "Description", tk.TypeId);
            return PartialView("Edit", tk);
        }

        [HttpPost]
        public ActionResult Edit(Ticket tk, FormCollection collection)
        {
            tk.CustomerId = CustomerBusiness.SelectIdbyName(collection["SearchCustomer"]);
            IFormatProvider iFP = new System.Globalization.CultureInfo("vi-VN", true);

            tk.StartTime = DateTime.Parse(collection["StartTime"], iFP);
            tk.EndTime = DateTime.Parse(collection["EndTime"], iFP);

            tk.MotorbikePlate = collection["SearchPlate"];
            tk.TypeId = Int64.Parse(collection["ticketType"]);
            tk.CustomerId = CustomerBusiness.SelectIdbyName(collection["Customer.FullName"]);
            TicketBusiness.Update(tk);
            ViewBag.ticketType = new SelectList(TicketTypeBusiness.ListTicketTypes(), "Id", "Description", tk.TypeId);
            return PartialView("Edit", tk);
        }

        [HttpPost]
        public ActionResult Delete(Int64 id, int? page)
        {
            TicketBusiness.Delete(id);
            int pageNumber = (page ?? 1);
            var ticket = TicketBusiness.Search(String.Empty);
            return PartialView("ListTicket", ticket.ToPagedList(pageNumber, 3));
        }
    }
}